package route

import (
	"gochat/route/handler"
	"gochat/route/middleware"

	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	r := gin.Default()
	r.Use(middleware.IsLoginToken)
	r.Use(func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(204)
			return
		}

		c.Next()
	})
	userRoute := r.Group("/user")
	{
		userRoute.POST("/register", handler.UserRegister)
		userRoute.PUT("/:id", handler.UpdateUser)
		userRoute.DELETE("/:id", handler.DeleteUser)
		userRoute.POST("/logout", handler.Logout)
		userRoute.GET("/online", handler.OnlineNumber)
		userRoute.GET("/:id", handler.GetUserInfo)
	}
	friend := r.Group("/friend")
	{
		friend.POST("/friendRequest", handler.FriendRequest)
		friend.POST("/acceptFriendReq/:id/:status", handler.AcceptFriendRequest)
		friend.GET("/list", handler.FriendList)
	}
	group := r.Group("/group")
	{
		group.POST("/create", handler.CreateGroup)
		group.GET("/list", handler.GroupList)
		group.DELETE("/:id", handler.DeleteGroup)
		group.PUT("/update/:id", handler.UpdateGroup)
	}
	r.GET("/ws/message", handler.Message)
	r.GET("/ws/friend/receiveFriendRequest", handler.ReceiveFriendRequest)
	r.POST("/auth/login", handler.UserLogin)
	r.GET("/auth/login/github/callback", handler.GithubOAuth)
	return r
}
